import { defineStore } from 'pinia'
import { reqLogin } from '../../api/user'
import type { loginForm, loginResponseData } from '../../api/type'
import type { UserState } from './types/type'
import { SET_TOKEN, GET_TOKEN } from '@/utils/token'

let userUserStore = defineStore('User', {
  state: (): UserState => {
    return {
      token: GET_TOKEN()
    }
  },
  actions: {
    async userLogin(data: loginForm) {
      console.log('登录')
      const res: loginResponseData = await reqLogin(data)
      console.log(res, 'mlpnko');
      if (res.code === 200) {
        this.token = (res.data.token as string)
        SET_TOKEN(res.data.token as string)
        return 'ok'
      } else {
        return Promise.reject(new Error(res.data.message))
      }

    },
  },
  getters: {},
})
export default userUserStore
